import redis

"""
有序集合：主要用于各种排行榜
"""

r = redis.Redis(host='127.0.0.1', port=6379, db=2)

r.zadd("rank1", {"彩虹": 200, "给我一首歌的时间": 500})
r.zadd("rank2", {"孤勇者": 300, "十年": 400})

result = r.zrange("rank1", 0, -1, withscores=True)
for i in result:
    print(i[0].decode(), int(i[1]))

# 不带权重，并集
r.zunionstore("rank3", ["rank1", "rank2"], aggregate="SUM")
result = r.zrange("rank3", 0, -1, withscores=True)
for i in result:
    print(i[0].decode(), int(i[1]))

# 带权重
r.zunionstore("rank4", {'rank1': 0.5, 'rank2': 0.5}, aggregate="MAX")
result = r.zrange("rank4", 0, -1, withscores=True)
for i in result:
    print(i[0].decode(), int(i[1]))
